﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YunSpace.Application.OrderFlowManage;
using YunSpace.Application.SystemManage;
using YunSpace.Core;
using YunSpace.Domain.ViewModel.OrderFlowManage;

namespace YunSpace.Web.Areas.CheckManager.Controllers
{
    public class CheckInfosController : ControllerBase
    {
        OrderCheckApp app = new OrderCheckApp();

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult SetPriceDetail()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public virtual ActionResult ComfireForm()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public virtual ActionResult HighSeasCheck()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public virtual ActionResult OrderPriceView()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public virtual ActionResult Salas()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public virtual ActionResult OrderPriceCheck()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public virtual ActionResult CreateOrderPrice()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public virtual ActionResult MyApply()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public virtual ActionResult MyCheck()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public virtual ActionResult UsingCheck()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public virtual ActionResult OrderIntentionCheck()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public virtual ActionResult ComfireIntentiton()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public virtual ActionResult OrderPriceDbCheck()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public virtual ActionResult OrderPriceEdit()
        {
            return View();
        }

        [HttpGet]
        public ActionResult GetGridData(Pagination pagination, string queryJson)
        {
            var data = new
            {
                rows = app.GetOrderCheckList(pagination, queryJson, ""),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult GetHighSeaCheckList(Pagination pagination)
        {
            var data = new
            {
                rows = app.GetHighSeaCheckList(pagination),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult GetMyApplyGridData(Pagination pagination, string queryJson)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            var data = new
            {
                rows = app.GetMyApplyGridData(pagination, queryJson, LoginInfo.UserId),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult GetMyCheckGridData(Pagination pagination, string queryJson)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            var data = new
            {
                rows = app.GetOrderCheckList(pagination, queryJson, LoginInfo.UserId),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult GetCheckSchedule(Pagination pagination, string queryJson)
        {
            var data = new
            {
                rows = app.GetCheckSchedule(pagination, queryJson),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult GetCheckIntention(Pagination pagination, string queryJson)
        {
            var data = new
            {
                rows = app.GetOrderIntentionModel(pagination, queryJson),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult GetCheckPrice(string queryJson)
        {
            var data = app.GetCheckPrice(queryJson);
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult GetItemListJson()
        {
            var data = new
            {
                F_ApplyType = app.GetItemList("sqlx"),
                Organize = app.GetOrganizeList(),
                Users = app.GetUserList()
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult GetItemList()
        {
            var data = new
            {
                wdqly = app.GetItemList("wdqly"),
                City = app.GetAllCitys(),
                units = app.GetItemList("units")
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult GetItemsJson()
        {
            var data = new
            {
                City = app.GetAllCitys()
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult GetHighSeaItemJson()
        {
            var data = new
            {
                resource = app.GetItemList("xqly"),
                F_ActivityType = app.GetItemList("ActivityType"),
                F_SiteType = app.GetItemList("SiteType")
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult GetSitePrice(string F_Id)
        {
            var Space = app.GetSitePrice(F_Id);
            var unit = app.GetItemList("units");
            return Content((new { YPrice = Space.F_YunPrice, MPrice = Space.F_MarketPrice,units= unit }).ToJson());
        }

        [HttpPost]
        public ActionResult SetSchedule(string F_Id, int State, string F_CheckId, string F_FlowId, int ApplyType, string F_CheckUserId,string YPrice,string MPrice,string unit)
        {
            if (!app.OpreationFlowCheck(F_FlowId, ApplyType, F_CheckId))
                return Error("上一审批人还未审批");
            else
            {
                app.SetSchedule(F_Id, State, F_CheckId, F_FlowId, ApplyType, F_CheckUserId,MPrice,YPrice, unit);
                if (State == 1)
                    app.OpreationSignRecord(F_FlowId, ApplyType, F_CheckId);
                return Success("操作成功。");
            }
        }

        [HttpPost]
        public ActionResult SubmitFormCheck(OrderCheckScheduleModel check)
        {
            if (!app.OpreationFlowCheck(check.F_FlowId, check.ApplyType, check.F_CheckId))
                return Error("上一审批人还未审批");
            else
            {
                app.SubmitFormCheck(check.F_Id, check.F_CheckId, check.F_Reason, check.F_Remark, check.F_FlowId, check.ApplyType, check.F_CheckUserId);
                //app.OpreationSignRecord(check.F_FlowId, check.ApplyType, check.F_CheckId);
                return Success("操作成功。");
            }
        }

        [HttpPost]
        public ActionResult SubmitCheckHighSea(string F_Id, string F_IsCheck)
        {
            app.SubmitCheckHighSea(F_Id, F_IsCheck);
            return Success("操作成功。");
        }

        [HttpPost]
        public ActionResult SubmitFormIntention(OrderCheckScheduleModel check)
        {
            app.SubmitFormIntention(check.F_Id, check.F_CheckId, check.F_Reason, check.F_Remark, check.F_FlowId, check.ApplyType, check.F_CheckUserId);
            return Success("操作成功。");
        }

        [HttpGet]
        public ActionResult CheckOrder(string F_Id, int State)
        {
            app.CheckOrder(F_Id, State);
            return Success("操作成功。");
        }

        [HttpPost]
        public ActionResult CheckOrderIntention(string F_Id, int State, string F_CheckId, string F_FlowId, int ApplyType, string F_CheckUserId)
        {
            app.CheckOrderIntention(F_Id, State, F_CheckId, F_FlowId, ApplyType, F_CheckUserId);
            return Success("操作成功。");
        }

        #region 报价单
        [HttpGet]
        public ActionResult GetOrderPriceInfos(string F_Ids)
        {
            var data = app.GetOrderPriceInfos(F_Ids);
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult GetOrderPriceView(string F_OrderId, string F_No)
        {
            var data = app.GetOrderPriceView(F_OrderId, F_No);
            return Content(data.ToJson());
        }

        [HttpPost]
        public ActionResult CheckOrderPrice(int ApplyType, string F_CheckId, int F_State, string F_CheckUserId, string F_FlowId, string F_OrderId, string F_No)
        {
            if (!app.OpreationFlowCheck(F_FlowId, ApplyType, F_CheckId))
                return Error("上一审批人还未审批");
            else
            {
                app.CheckOrderPrice(ApplyType, F_CheckId, F_State, F_CheckUserId, F_FlowId, F_OrderId, F_No);
                if (F_State == 1)
                    app.OpreationSignRecord(F_FlowId, ApplyType, F_CheckId);
                return Success("操作成功。");
            }
        }

        [HttpPost]
        public ActionResult CheckDbOrderPrice(int ApplyType, string F_CheckId, int F_State, string F_CheckUserId, string F_FlowId, string F_OrderId, string F_No)
        {
            if (!app.OpreationFlowCheck(F_FlowId, ApplyType, F_CheckId))
                return Error("上一审批人还未审批");
            else
            {
                app.CheckDbOrderPrice(ApplyType, F_CheckId, F_State, F_CheckUserId, F_FlowId, F_OrderId, F_No);
                if (F_State == 1)
                    app.OpreationSignRecord(F_FlowId, ApplyType, F_CheckId);
                return Success("操作成功。");
            }
        }

        [HttpPost]
        public ActionResult InsertOrderPrice(OrderPriceInput result, string F_CheckId, string F_OrderId, string F_FlowId, int ApplyType, string F_No)
        {
            if (!app.OpreationFlowCheck(F_FlowId, ApplyType, F_CheckId))
                return Error("上一审批人还未审批");
            else
            {
                app.InsertOrderPrice(result, F_CheckId, F_OrderId, F_FlowId, ApplyType, F_No);
                app.OpreationSignRecord(F_FlowId, ApplyType, F_CheckId);
                return Success("操作成功。");
            }
        }

        [HttpPost]
        public ActionResult EditOrderPrice(string F_Id, decimal F_YunPrice, int F_Quantity, decimal F_TotalPrice)
        {
            app.EditOrderPrice(F_Id, F_YunPrice, F_Quantity, F_TotalPrice);
            return Success("操作成功。");
        }
        #endregion

        #region 场地带看

        /// <summary>
        /// 场地带看详情
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult OrderTakelookDetail()
        {
            return View();
        }

        /// <summary>
        /// 获取场地带看详情列表
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetOrderTakelookDetailGridJson(Pagination pagination, string keyValue, string F_CheckUserId, string F_No)
        {
            var data = new
            {
                rows = app.GetOrderTakelookDetailViewModels(pagination, keyValue, F_CheckUserId, F_No),
                pagination.total,
                pagination.page,
                pagination.records,
            };

            return Content(data.ToJson());
        }

        /// <summary>
        /// 确认带看
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult ConfirmTakeLook(string orderTakelookId, int state, int reason, string remark, string checkId, string F_FlowId, int ApplyType, string F_CheckUserId)
        {
            if (!app.OpreationFlowCheck(F_FlowId, ApplyType, checkId))
                return Error("上一审批人还未审批");
            else
            {
                app.ConfirmTakeLook(orderTakelookId, state, reason, remark, checkId, F_FlowId, ApplyType, F_CheckUserId);
                if (state == 1)
                    app.OpreationSignRecord(F_FlowId, ApplyType, checkId);
                return Success("成功");
            }
        }

        /// <summary>
        /// 驳回带看表单
        /// </summary>
        /// <returns></returns>
        public ActionResult RejectTakeLookForm()
        {
            return View();
        }

        [HttpGet]
        public ActionResult GetRejectTakeLookFormDictionaryItemsJson()
        {
            var data = new
            {
                bhly = app.GetItemList("bhly")
            };

            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult GetUsingDictionaryItemsJson()
        {
            var data = new
            {
                sshy = app.GetItemList("sshy"),
                City = app.GetAllCitys(),
                hdlx = app.GetItemList("ActivityType"),
                fylm = app.GetItemList("fylm")
            };

            return Content(data.ToJson());
        }

        #endregion 场地带看

        #region 场地预定

        /// <summary>
        /// 场地预定详情
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult OrderReserveDetail()
        {
            return View();
        }

        /// <summary>
        /// 获取场地预定详情列表
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetOrderReserveDetailGridJson(Pagination pagination, string keyValue, string F_CheckUserId, string F_No)
        {
            var data = new
            {
                rows = app.GetOrderReserveViewModelViewModels(pagination, keyValue, F_CheckUserId, F_No),
                pagination.total,
                pagination.page,
                pagination.records,
            };

            return Content(data.ToJson());
        }

        /// <summary>
        /// 确认场地预定
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult ConfirmReserve(string orderReserveId, int state, int reason, string remark, string checkId, string F_FlowId, int ApplyType, string F_CheckUserId)
        {
            if (!app.OpreationFlowCheck(F_FlowId, ApplyType, checkId))
                return Error("上一审批人还未审批");
            else
            {
                app.ConfirmReserve(orderReserveId, state, reason, remark, checkId, F_FlowId, ApplyType, F_CheckUserId);
                if (state == 1)
                    app.OpreationSignRecord(F_FlowId, ApplyType, checkId);
                return Success("成功");
            }
        }

        /// <summary>
        /// 驳回场地预定表单
        /// </summary>
        /// <returns></returns>
        public ActionResult RejectReserveForm()
        {
            return View();
        }

        /// <summary>
        /// 获取场地预定字典项
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetRejectReserveDictionaryItemsJson()
        {
            var data = new
            {
                bhly = app.GetItemList("bhly")
            };

            return Content(data.ToJson());
        }

        #endregion 场地预定

        #region 合同审核

        /// <summary>
        /// 合同审核详情
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult OrderSignContractDetail()
        {
            return View();
        }

        /// <summary>
        /// 获取合同审核详情列表
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetOrderSignContractDetailGridJson(Pagination pagination, string keyValue, string userId, string F_No)
        {
            var data = new
            {
                rows = app.GetOrderSignContractViewModelViewModels(pagination, keyValue, userId, F_No),
                pagination.total,
                pagination.page,
                pagination.records,
            };

            return Content(data.ToJson());
        }

        /// <summary>
        /// 确认合同审核
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult ConfirmSignContract(string orderSignContractId, int state, string remark, string checkId, string F_FlowId, int ApplyType, string F_CheckUserId)
        {
            if (!app.OpreationFlowCheck(F_FlowId, ApplyType, checkId))
                return Error("上一审批人还未审批");
            else
            {
                app.ConfirmSignContract(orderSignContractId, state, remark, checkId, F_FlowId, ApplyType, F_CheckUserId);
                if (state == 1)
                    app.OpreationSignRecord(F_FlowId, ApplyType, checkId);
                return Success("成功");
            }
        }

        /// <summary>
        /// 驳回合同审核表单
        /// </summary>
        /// <returns></returns>
        public ActionResult RejectSignContractForm()
        {
            return View();
        }

        #endregion 合同审核

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetOrderUsing(string F_CheckId, string F_OrderId)
        {
            var data = app.GetOrderUsing(F_CheckId, F_OrderId);
            return Content(data.ToJson());
        }
        /// <summary>
        /// 确认合同审核
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CheckUsing(int F_State, string F_CheckId, string F_FlowId, int ApplyType, string F_CheckUserId)
        {
            if (!app.OpreationFlowCheck(F_FlowId, ApplyType, F_CheckId))
                return Error("上一审批人还未审批");
            else
            {
                app.CheckUsing(F_CheckId, F_State, F_CheckId, F_FlowId, ApplyType, F_CheckUserId);
                if (F_State == 1)
                    app.OpreationSignRecord(F_FlowId, ApplyType, F_CheckId);
                return Success("成功");
            }
        }

        #region ERROR ERROR: Table 'YunSpaceCrmTest.salas' doesn't exist GaoTang 2018.05.09
        // ERROR: Table 'YunSpaceCrmTest.salas' doesn't exist
        // GaoTang 2018.05.09

        SalasApp salaapp = new SalasApp();
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSalaList(Pagination pagination, string queryJson)
        {
            var data = new
            {
                rows = salaapp.GetList(),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult OperationSalas()
        {
            salaapp.OperationSalas();
            return Success("成功");
        }
        #endregion

        #region 会签
        public ActionResult SignRecord()
        {
            return View();
        }

        public ActionResult UsingSign()
        {
            return View();
        }

        public ActionResult MySign()
        {
            return View();
        }

        public ActionResult GetSignGridData(Pagination pagination)
        {
            var data = new
            {
                rows = app.GetOrderSignList(pagination,""),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        public ActionResult GetMySignGridData(Pagination pagination)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            var data = new
            {
                rows = app.GetOrderSignList(pagination, LoginInfo.UserId),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult AddSignContent(string SignContent, string F_Id)
        {
            app.AddSignContent(SignContent, F_Id);
            return Success("操作成功");
        }
        #endregion

        #region 我的申请查看页面
        [HttpGet]
        [HandlerAuthorize]
        public ActionResult DetailApply()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult OrderTakelookDetailApply()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult OrderPriceCheckApply()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult OrderPriceDbCheckApply()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult OrderReserveDetailApply()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult UsingCheckApply()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult OrderSignContractDetailApply()
        {
            return View();
        }
        #endregion
    }
}